package rickyGame.server.state
{
	import flash.geom.Point;
	
	import ricky.game.Actor;
	import ricky.game.action.Act2D;
	
	import rickyGame.model.*;
	import rickyGame.model.config.*;
	import ricky.game.Actor2D;
	import rickyGame.game.Fighter;
	import rickyGame.game.GameState;
	import rickyGame.game.Player;
	import rickyGame.game.action.ai.IdleAi;
	import rickyGame.game.factory.UnitFactory;
	
	/**
	 * 创建军队
	 */
	public class CreateArmy extends Act2D
	{
		public function CreateArmy(actor:Actor,pars:Array,point:Point)
		{
			super(actor);
			_group=actor.group;
			_units=pars;
//			_units=pars[0];
//			_nums=pars[1];
//			_level=pars[2];
			_army=new Array();
		}
		
		override public function begin():void
		{
			createArmyByUnit();
			this.complete();
		}
		
		private function createArmyByUnit():void
		{
			_army.length=0;
			var p:Actor2D;
			var unit:Unit,level:int,num:int,time:int;
			var gunit:Array;
			for(var i:int=0;i<_units.length;i++)
			{
				gunit=_units[i];
				unit=ConfigUnitHelper.getUnitConfig(gunit[0]);
				if(!unit)
					continue;
				num=gunit[1];
				level=gunit[2];
				time=num;
				while(time>0)
				{
					time--;
					p=UnitFactory.CreateActor(unit,level);
					p.group=_group;
					p.graphic.x=actor.graphic.x+Math.random()*80;
					p.graphic.y=actor.graphic.y+Math.random()*80;
					if(p is Fighter)
					{
						Fighter(p).ai.localX=p.graphic.x;
						Fighter(p).ai.localY=p.graphic.y;
					}
					if(p.group==0)
						p.direction=4;
					else
						p.direction=0;
					p.calcMorton();
					p.name="<font color='#a0dc2a' size='12' >"+unit.name+"</font>";
					//				
					if(p is Player)
					{
						p.defaultAct=new IdleAi(p);
						p.addAct(Act2D(p.defaultAct));
					}
	
					_army.push(p);
				}
			}
		}

		public function get army():Array
		{
			return _army;
		}

		//
		private var _army:Array;
		private var _point:Point;
		private var _group:int;
		private var _units:Array;//单位id
//		private var _nums:Array;//数量
//		private var _level:Array;
	}
}